package com.example.RxJavaDemo;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;


public abstract class BaseActivity<V extends ViewDataBinding> extends Activity {

    protected V mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        callBeforeSetContentView(savedInstanceState);

        if (getContentView() != null) {
            mBinding = DataBindingUtil.bind(getContentView());
        } else if (getContentViewLayoutID() != 0) {
            mBinding = DataBindingUtil.inflate(LayoutInflater.from(this), getContentViewLayoutID(), null, false);
        }
        if (mBinding != null) {
            setContentView(mBinding.getRoot());
        }
        initData();
        initView(savedInstanceState);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackClick();
        }
        return super.onOptionsItemSelected(item);
    }

    protected void callBeforeSetContentView(Bundle savedInstanceState) {

    }

    protected View getContentView() {
        return null;
    }

    protected abstract int getContentViewLayoutID();

    protected abstract void initData();

    protected abstract void initView(Bundle savedInstanceState);

    protected void onBackClick() {
        finish();
    }
}

